<template>
	<view class="container">
		<u--form :model="form" ref="uForm" :rules="rules" labelWidth="0">
			<view class="form-subtitle">
				<view class="form-subtitle-left">
					基本信息
				</view>
			</view>
			<view class="form-container">
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">类型<span class="label-required">*</span></span>
						<u-radio-group v-model="form.type" activeColor="#FE4F04">
							<u-radio :name="1" label="个人"> 个人 </u-radio>
							<u-radio :name="2" label="企业"> 企业 </u-radio>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item prop="name" v-if="form.type=='1'">
					<view class="form-flex">
						<span class="form-label">姓名<span class="label-required">*</span></span>
						<u--input v-model="form.name" placeholder="请输入姓名" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="name" v-if="form.type=='2'">
					<view class="form-flex">
						<span class="form-label">名称<span class="label-required">*</span></span>
						<u--input v-model="form.name" placeholder="请输入公司名称" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item prop="contact_name" v-if="form.type=='2'">
					<view class="form-flex">
						<span class="form-label">联系人</span>
						<u--input v-model="form.contact_name" placeholder="请输入公司联系人姓名" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">性别</span>
						<u-radio-group v-model="form.sex" activeColor="#FE4F04" class="u-radio-group--row">
							<u-radio :name="1" label="男"> 男 </u-radio>
							<u-radio :name="2" label="女"> 女 </u-radio>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">手机号<span class="label-required">*</span></span>
						<u--input v-model="form.mobile" placeholder="请输入手机号" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">证件号码</span>
						<u--input v-model="form.id_card" placeholder="请输入证件号码" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex" style="margin-bottom:20rpx;">
						<span class="form-label">证件照</span>
						<span>{{imgList.length}}/2</span>
					</view>
					<u-upload uploadIcon="photo" uploadIconColor="#FE4F04" :fileList="imgList" @afterRead="afterRead"
						@delete="deletePic" :maxCount="2" :previewImage="true"></u-upload>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">备注</span>
						<u-input v-model="form.remark" placeholder="请输入备注" inputAlign="right" border="none" />
					</view>
				</u-form-item>
			</view>

			<view class="form-container" v-if="!switchVal">
				<u-form-item label="其他信息" labelWidth="100">
					<u-switch slot="right" v-model="switchVal" activeColor="#FE4F04" size="18"></u-switch>
				</u-form-item>
			</view>

			<view class="form-subtitle" v-if="switchVal">
				<view class="form-subtitle-left">
					其他信息
				</view>
				<view class="form-subtitle-right">
					<u-switch slot="right" v-model="switchVal" activeColor="#FE4F04" size="18"></u-switch>
				</view>
			</view>
			<view class="form-container" v-if="switchVal">
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">备用号码</span>
						<u--input v-model="form.backup_mobile" placeholder="请输入备用号码" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">电子邮箱</span>
						<u--input v-model="form.email" placeholder="请输入电子邮箱" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">户籍地址</span>
						<u--input v-model="form.card_address" placeholder="请输入户籍地址" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">职业</span>
						<u--input v-model="form.job" placeholder="请输入职业" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">工作单位</span>
						<u--input v-model="form.company_name" placeholder="请输入工作单位" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">工作地址</span>
						<u--input v-model="form.company_address" placeholder="请输入工作地址" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">车牌号</span>
						<u--input v-model="form.car_no" placeholder="请输入车牌号" inputAlign="right" border="none" />
					</view>
				</u-form-item>
			</view>
			<view class="form-subtitle" v-if="switchVal">
				<view class="form-subtitle-left">
					紧急联系人
				</view>
			</view>
			<view class="form-container" v-if="switchVal">

				<u-form-item>
					<view class="form-flex">
						<span class="form-label">姓名</span>
						<u--input v-model="form.ji_name" placeholder="请输入姓名" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">电话</span>
						<u--input v-model="form.ji_mobile" placeholder="请输入电话" inputAlign="right" border="none" />
					</view>
				</u-form-item>
			</view>

			<view class="form-bottom-opt">
				<view class="opt-lh" v-if="form.id && form.id != '' && form.is_black != '1'" @click="handleLh">
					<img src="@/pages-landlord/static/images/black.png" alt="" class="opt-icon"/>
					<span>拉黑</span>
				</view>
				<view class="opt-del" v-if="form.id && form.id != ''" @click="handleDel">
					<img src="@/pages-landlord/static/images/del.png" alt="" class="opt-icon"/>
					<span>删除</span>
				</view>
				<view class="opt-zd" v-if="form.id && form.id != ''" @click="handleZd">
					<img src="@/pages-landlord/static/images/bill.png" alt="" class="opt-icon"/>
					<span>查看账单</span>
				</view>
				<u-button v-if="form.is_black != '1'" @click="submit" :custom-style="submitBtnStyle">提交</u-button>
			</view>
		</u--form>

		<u-modal :show="delShow" title="确认要删除此客户吗？" :showCancelButton="true" @cancel="delShow = false" @confirm="delConfirm"
			confirmColor="#FE4F04"></u-modal>

		<u-popup :show="blackShow" round="40" @close="blackShow = false">
			<view class="popup-form">
				<view class="popup-form-label">
					拉黑类型<span class="required">*</span>
				</view>
				<view class="popup-form-black-type-list">
					<text class="popup-form-black-type-item" :class="blackForm.black_type == key ? 'active' : ''"
						v-for="(value, key) in blackTypeList" @click="blackForm.black_type = key">
						{{value}}
					</text>
				</view>
				<view class="popup-form-label">
					原因<span class="required">*</span>
				</view>
				<view style="background-color: #F5F6FA;margin-bottom: 20rpx;border-radius: 20rpx;">
					<u--textarea v-model="blackForm.black_reason" placeholder="请输入内容" inputAlign="right" border="none" maxlength="-1"/>
				</view>
				<u-button @click="submitBlack" :custom-style="submitBtnStyle">提交</u-button>
			</view>
		</u-popup>
	</view>
</template>


<script>
	import {
		customerAdd,
		customerInfo,
		customerDel,
		uploadFile,
		customerBlackTypeList,
		customerBlack
	} from "@/api/part2"
	export default {
		data() {
			return {
				form: {
					id: '',
					type: '1',
					name: '',
					contact_name:'',
					sex: '',
					mobile: '',
					id_card: '',
					card_image: [],
					remark: '',
					backup_mobile: '',
					email: '',
					card_address: '',
					job: '',
					company_name: '',
					company_address: '',
					car_no: '',
					ji_name: '',
					ji_mobile: ''
				},
				imgList: [],
				switchVal: false,
				delShow: false,
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'type': {
						type: 'string',
						required: true,
						message: '请选择类型',
						trigger: ['change']
					},
					'mobile': {
						type: 'string',
						required: true,
						message: '请填写手机号',
						trigger: ['blur']
					},
				},
				blackShow: false,
				blackTypeList: {},
				blackForm: {
					id: '',
					black_type: '',
					black_reason: '',
				},
				submitBtnStyle: {
					background: "#FE4F04",
					boShadow: "0rpx 4rpx 8rpx 0rpx rgba(254,79,4,0.5)",
					borderRadius: "44rpx",
					fontSize: "30rpx",
					color: "#FFFFFF",
					height: "88rpx",
				}
			};
		},
		onLoad(options) {
			if (options.id) {
				this.form.id = options.id
				this.switchVal = true
				this.getcustomerInfo()
				this.customerBlackTypeList()
			}
		},
		methods: {
			getcustomerInfo() {
				customerInfo(this.form.id).then(res => {
					console.log('客户信息：', res.data.data)
					let data = res.data.data
					this.form = {
						...data,
						card_image:data.card_image!= '' ? data.card_image.split(',') : []
					}
					this.imgList = this.form.card_image.map(url => ({
						url: url,
						// 其他可选属性
						status: 'success',
						message: '上传成功'
					}));
				})
			},
			customerBlackTypeList() {
				customerBlackTypeList().then(res => {
					console.log('拉黑类型：', res.data.data)
					this.blackTypeList = res.data.data
				})
			},
			handleLh() {
				this.blackShow = true
				this.blackForm = {
					id: this.form.id,
					black_type: '',
					black_reason: '',
				}
			},
			handleDel() {
				this.delShow = true
			},
			handleZd() {
				uni.navigateTo({
					url: '/pages-landlord/house/receiptList?kw='+this.form.mobile
				})
			},
			afterRead(e) {
				uni.showLoading({
					title: "上传中...",
				});
				this.imgList.push(e.file);
				uploadFile(e.file.url).then((res) => {
					uni.hideLoading();
					this.form.card_image.push(JSON.parse(res.data).code.fullurl);
				});
			},
			deletePic(e) {
				this.imgList.splice(e.index, 1);
				this.form.card_image.splice(e.index, 1);
			},
			submit() {
				console.log('提交')
				this.$refs.uForm.validate().then(res => {
					this.form.card_image = this.form.card_image.join(',')
					customerAdd(this.form).then(res => {
						uni.$u.toast(res.msg || '添加成功')
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			delConfirm() {
				customerDel({
					id: this.form.id
				}).then(res => {
					uni.$u.toast(res.msg || '删除成功')
					setTimeout(() => {
						uni.navigateBack()
					}, 800)
				})
			},
			submitBlack(){
				if(this.blackForm.black_type == ''){
					uni.$u.toast('请输入拉黑类型')
					return
				}
				if(this.blackForm.black_reason == ''){
					uni.$u.toast('请输入原因')
					return
				}
				customerBlack(this.blackForm).then(res=>{
					uni.$u.toast(res.msg || '拉黑成功')
					this.blackShow = false
					setTimeout(() => {
						uni.navigateBack()
					}, 800)
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	};
</script>

<style lang="less" scoped>
	view, span, text {
		box-sizing: border-box;
	}
	.container {
		padding: 0 32rpx;
		padding-bottom: 150rpx;
		margin-bottom: constant(safe-area-inset-bottom);
		margin-bottom: env(safe-area-inset-bottom);
		background: #F5F6FA;
		min-height: 100vh;
	}

	.form-subtitle {
		padding: 24rpx 40rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 700;
		font-size: 26rpx;
		color: #2A0C00;
		line-height: 36rpx;

		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.form-container {
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
		border-radius: 16rpx;
		padding: 0 40rpx;
		margin-bottom: 20rpx;

		.form-flex {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.form-label {
			margin-right: 10rpx;

			.label-required {
				color: #FF0000;
				padding: 0 4rpx;
			}
		}
	}

	.form-bottom-opt {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		/* 关键样式 - 使用 constant 和 env 兼容不同设备 */
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		/* 其他样式按需添加 */
		background-color: #F5F6FA;
		padding: 20rpx 40rpx;

		display: flex;
		gap: 48rpx;
		justify-content: space-around;

		.opt-icon {
			width: 32rpx;
			height: 32rpx;
		}
		.opt-lh {
			min-width: 44rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 22rpx;
			color: #333333;
			line-height: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 44px;
		}

		.opt-del {
			min-width: 44rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 22rpx;
			color: #333333;
			line-height: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 44px;
		}

		.opt-zd {
			min-width: 100rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 22rpx;
			color: #333333;
			line-height: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 44px;
		}
	}

	.popup-form-label {
		font-weight: 500;
		font-size: 32rpx;
		color: #222222;
		line-height: 32rpx;
		padding: 32rpx 0;

		.required {
			color: red;
			padding: 0 5rpx;
		}
	}
	.popup-form {
		padding: 40rpx;
	}
	.popup-form-black-type-list {
		display: flex;
		flex-wrap: wrap;
		gap: 30rpx 24rpx;
		justify-content: space-between;

		.popup-form-black-type-item {
			flex-grow: 1; /* 自动填充剩余空间 */
			min-width: fit-content; /* 宽度至少适应内容 */
			
			padding: 0 32rpx;
			height: 80rpx;
			min-width: 204rpx;
			font-weight: 400;
			font-size: 28rpx;
			color: #222222;
			background: #F5F6FA;
			border-radius: 40rpx;
			border: 1rpx solid #F5F6FA;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.popup-form-black-type-item.active {
			color: #FF7D41;
			background: #FFF6ED;
			border: 1rpx solid #FE4F04;
		}
	}

	/deep/ .u-form-item {
		justify-content: flex-end !important;
		/* 右对齐 */
	}

	/deep/ .u-radio-group--row {
		gap: 30rpx;
		justify-content: flex-end;
		align-items: center;
	}
	/deep/ .u-textarea {
		background-color: #F5F6FA !important;
	}
</style>